<?php

namespace app\service;


use app\model\HighShop;
use app\model\UserBackpack;
use app\model\UserOrder;
use app\model\UserWatchAdLog;
use extend\Random;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;

//至臻商店服务
class HighShopService extends BaseService
{

    //福袋内容
    protected function gift(): array
    {
        return [
            ['type' => GET_METHOD['至臻点'], 'num' => 1],  //至臻点1点,
            ['type' => GET_METHOD['至臻点'], 'num' => 2], //至臻点2点,
            ['type' => GET_METHOD['钻石'], 'num' => 300], //300钻石
            ['type' => GET_METHOD['服装抽奖卷'], 'num' => 1],  //一张服装抽奖卷
        ];
    }

    /**
     * @throws ModelNotFoundException
     * @throws DataNotFoundException
     * @throws DbException
     */
    public function watch($user_id): array
    {
        //查询我的观看记录
        $user_ad_log_list = (new UserWatchAdLog())->where('user_id', $user_id)->order('created_at desc')->whereTime('created_at', 'today')->select();

        if ($user_ad_log_list->count() > HIGH_SHOP_CONFIG['每日最大购买次数']) {
            return $this->returnFail('今日观看次数已达上限');
        }

        if (!empty($user_ad_log_list[0]) && $user_ad_log_list[0]->created_at > date('Y-m-d H:i:s', strtotime("-" . HIGH_SHOP_CONFIG['每次间隔时间'] . " minute"))) {
            return $this->returnFail('观看时间间隔未到');
        }

        //添加观看记录

        $user_ad_log = new UserWatchAdLog();
        $user_ad_log->user_id = $user_id;
        $user_ad_log->channel = AD_CHANNEL['至臻商店'];
        $user_ad_log->save();

        //发放奖励  至臻点加一
        $res = (new UserService())->change_money($user_id, 1, USER_CAPITAL_TYPE['至臻点'], USER_CAPITAL_STATUS['增加']);
        if (!$res['code']) {
            return $res;
        }

        //发放福袋
        $gift = $this->gift();
        //在福袋列表里随机获取一个奖励
        $gift = $gift[array_rand($gift)];
        if ($gift['type'] == GET_METHOD['服装抽奖卷']) {
            //向背包放一个服装抽奖卷
            $res = (new UserBackpackService())->add($user_id, $gift['type'], USER_BACKPACK_CATEGORY['消耗品'], $gift['type'], $gift['num']);
        } else {
            $res = (new UserService())->change_money($user_id, $gift['num'], $gift['type'], USER_CAPITAL_STATUS['增加']);
        }

        if (!$res['code']) {
            return $res;
        }

        return $this->returnSuccess($gift);
    }


    /**
     * @throws ModelNotFoundException
     * @throws DataNotFoundException
     * @throws DbException
     */
    public function reward($user_id, $high_shop_id): array
    {
        //查询本周的观看记录
        $ad_count = (new UserWatchAdLog())->where('user_id', $user_id)->order('created_at desc')->whereTime('created_at', 'week')->count();

        //至臻商店的奖励配置
        $high_shop = (new HighShop())->find($high_shop_id);

        //用户已经领取的奖励
        $user_order_count = (new UserOrder())->where('user_id', $user_id)->where('shop_type', SHOP_TYPE['至臻商店'])->where('shop_id', $high_shop_id)->whereTime('created_at', 'week')->count();

        if ($user_order_count) {
            return $this->returnFail('本周已领取过该奖励');
        }

        if ($ad_count < $high_shop->count) {
            return $this->returnFail('未达到领取条件');
        }

        //生成订单
        $user_order = new UserOrder();
        $user_order->user_id = $user_id;
        $user_order->shop_id = $high_shop->id;
        $user_order->price = 0;
        $user_order->ptype = 10;
        $user_order->channel = 'hot_shop';
        $user_order->shop_type = SHOP_TYPE['至臻商店'];
        $user_order->num = 1;
        $res = $user_order->save();


        if (!$res) {
            return $this->returnFail('领取失败');
        }

        return $this->returnSuccess($user_order->toArray());


    }


}